<?php
class SaveComment extends Action {
	/*
	 * (non-PHPdoc) @see Action::execute()
	 */
	public function execute($params) {
		check_login ( ADMIN_LOGIN_PAGE );
		if (rqset ( 'reply' )) {
			return $this->reply ();
		} else {
			return $this->save ();
		}
	}
	// 新增回复
	private function reply() {
		$data ['success'] = false;
		$comment_id = irqst ( 'comment_id' );
		if (empty ( $comment_id )) {
			$data ['msg'] = '未指定要回复的评论';
		} else {
			$cmM = new WebCommentModel ();
			$cmt = $cmM->read ( $comment_id );
			if (! $cmt) {
				$data ['msg'] = '要回复的评论已经不存在.';
			} else {
				$I = whoami();
				$comment ['reply_id'] = $comment_id;
				$comment ['reply_author'] = $cmt ['author'];
				$comment ['page_id'] = $cmt ['page_id'];
				$comment ['create_uid'] = $I->uid;
				$comment ['author'] = $I->name;
				$comment ['email'] = $I->email;
				$comment ['status'] = 'pass';
				$comment ['url'] = CoreOptionModel::getOption ( 'site_url' );
				$comment ['comment'] = rqst ( 'comment' );
				$comment ['source_ip'] = $_SERVER ['REMOTE_ADDR'];
				$rst = $cmM->insert ( $comment );
				if (! $rst) {
					$data ['msg'] = '回复评论失败:' . $cmM->last_error_msg ();
				} else {
					$data ['success'] = true;
				}
			}
		}
		return new JsonView ( $data );
	}
	// 保存
	private function save() {
		$comment = rqsts ( array ('comment_id','author','email','url','status' => "new",'comment' ), true );
		$cmtModel = new WebCommentModel ();
		$comment ['comment'] = $comment ['comment'];
		$rst = $cmtModel->save ( $comment );
		if (! empty ( $rst )) {
			sess_add ( 'page_tip_info', '<strong>恭喜,</strong>评论已经保存完成.' );
			Response::redirect ( sess_get ( 'comment_list_url' ) );
		} else {
			sess_add ( 'comment_form_data', $comment );
			sess_add ( 'page_tip_info', '<strong>出错啦!</strong>' . $cmtModel->last_error_msg () );
			Response::redirect ( './?Ctlr=EditComment&cid=' . $comment ['comment_id'] );
		}
	
	}
}